﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class control_header : System.Web.UI.UserControl
{
    int catalogId = 0;
    int projectId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["id_user"] != null && Session["permission"] != null)
            {
                BindData();
                // display username
                lbusername.InnerHtml = "Xin chào " + Session["username"].ToString()+" !";
                // get project is selected
                try
                {
                    catalogId = Int32.Parse(Request.QueryString["catalog"]);
                }
                catch
                {
                    catalogId = 0;
                }
                if (catalogId != 0)
                {
                    //get projectId
                    string sql = "SELECT projectId FROM [catalog] WHERE id=" + comm.to_sql(catalogId.ToString(), "number");
                    DataTable dtTable = dal.SelectTable(sql);
                    if(dtTable.Rows.Count==1)
                    {
                        projectId = Int32.Parse(dtTable.Rows[0]["projectId"].ToString());
                    }
                }
                else
                {
                    try
                    {
                        projectId = Int32.Parse(Request.QueryString["project"]);
                    }
                    catch
                    {
                        projectId = 0;
                    }
                }
                try
                {
                    listproject.Items.FindByValue("0").Selected = false;
                    listproject.Items.FindByValue(projectId.ToString()).Selected = true;
                }
                catch
                {
                    listproject.Items.FindByValue("0").Selected = true;
                }
            }
            else
            {
                logout_click(sender, e);
            }
        }
    }

    public void BindData()
    {
        listproject.Items.Clear();
        ListItem aItem = new ListItem("-- Chọn dự án --", "0");
        listproject.Items.Add(aItem);

        // get list names of projects
        int id = Int32.Parse(Session["id_user"].ToString());
        SqlParameter param = new SqlParameter("@id_user", id);
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Input;
        DataTable dtTable = dal.SelectTableSP("proUser_ProjectName_SEL", param);
        Session.Add("projectnumber",dtTable.Rows.Count.ToString());
        Session.Timeout = 60;
        // display names of projects in dropdownlist
        foreach (DataRow row in dtTable.Rows)
        {
            ListItem item = new ListItem(row["name"].ToString(), row["id"].ToString());
            listproject.Items.Add(item);
        }
    }

    protected void logout_click(object sender, EventArgs e)
    {
        Session.Clear();
        Session.Abandon();
        Response.Redirect("~/login.aspx");
    }
    protected void listproject_SelectedIndexChanged(object sender, EventArgs e)
    {
        string project = ((DropDownList)sender).SelectedValue;
        if (project != "0")
            Response.Redirect("../home/Catalog.aspx?project=" + project);
        else
            Response.Redirect("../home/Default.aspx");
    }
}
